<?php

function translit($str){
    
}

require_once 'Dsql.php';

$maxLength = 44;

//------- facebook
$app_id = '178102922208034';
$canvas_page = 'http://apps.facebook.com/manyaphorisms/';
//------- facebook
$db = new Dsql("mysql-88.ukraine.com.ua", "barthez_ai", "x9gedv4w");

$db->selectDb('barthez_ai');
$db->selectCharset('utf8');


if(isset($_GET['order']) &&  $_GET['order'] == "top"){
    $aphorisms = $db->createQuery("SELECT * FROM `byone` where autorId in (SELECT id FROM aphoAuthors order by rate DESC)")
        ->setLimit(0, 8)
        ->getResult();
//        ->getSQL();


}else{

$aphorisms = $db->createQuery("SELECT * FROM `byone` order by rand()")
        ->setLimit(0, 8)
        ->getResult();
//        ->getSQL();
}




$alreadyArray = array();
?>
<html>
    <?include 'mainHeader.php'?>
    <head>
        <script type="text/javascript">

                    var _gaq = _gaq || [];
                    _gaq.push(['_setAccount', 'UA-19921719-1']);
                    _gaq.push(['_trackPageview']);

                    (function() {
                        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                    })();

                </script>
        <script src="http://vkontakte.ru/js/api/xd_connection.js?2" type="text/javascript"></script>
    </head>
    <body>

        <div class="mainContainer">
          
            <? include 'header.php';?>

            <div class="allcontent">
                
                <? foreach($aphorisms as $one):?>
                    
                    <? $feed_url = "http://www.facebook.com/dialog/feed?app_id=" 
                            . $app_id . "&redirect_uri=" . urlencode($canvas_page)
                            . "&message=" . $one['theme'].": ".$one['text'];    ?>
                
                    <div class="box">
                        <div class="bHead">
                            <?= $one['theme']?>

                            
                            
                        </div>
                        <div class="bBody" id="<?=$one['aphorismId']?>">
                             <?=$one['text']; ?>
                        </div>
                        <div class="bFoot">
                            <div class="element">
                                    
                                <a class="asJpeg hm10 a-gray <? if (!(strlen($one['text'])>40 && strlen($one['text'])<240)):?>hide<? endif;?>" href="./img-<?=rand(1000,9999)?>.png?text=<?=$one['text']?>&backId=<?=rand(1,8)?>">Картинка</a>
                                <a class="plusOne hm10 a-gray" href="javascript:void(0);" id="aph_<?=$one['aphorismId']?>">+1 автору</a>
                                
                                    
                                <a class="toWall hm10 a-gray" href="javascript:top.location.href='<?=$feed_url?>'">себе на стену</a>
                                
                                
                                <a class="moreThis hm10 a-gray" none="<?= $one['aphorismId']?>" id="<?= $one['autorId']?>"href="javascript:void(0);">еще этого автора</a>
                            </div>
                        </div>
                    </div>
                <? endforeach;?>
            </div>
            
        </div>
        
    </body>
</html>





<script type="text/javascript">
  
</script>

<script>
    
    function wo(url)
    {
        id=window.open("","_blank");
        id.location.href=url;
    }
    
    $(document).ready(function(){
        
        VK.init(function() {
             apiId: 2045241
          });
        $('.bFoot').hide();
        $('.box').each(function(){
            $(this).mouseenter(function(){
                $(this).children('.bFoot').fadeIn('slow');
                $(this).addClass('bordered');
            });
            $(this).mouseleave(function(){
                $(this).children('.bFoot').fadeOut('fast');
                $(this).removeClass('bordered');
            });
        });
        
        $('.plusOne').each(function(){
            var i = $(this);
            $(this).click(function(){
                $.ajax({
                    url: "./ajax.php?aphoId=" + $(this).attr('id'),
                    success: function(){
                        $(i).fadeOut('slow');
                    }
                });
            });
        });
        
        $('.moreThis').each(function(){
            var thisHref = $(this);
            var i = $(this).parent().parent().parent().children('.bBody');
            var foot = $(this).parent().parent().parent().children('.bFoot').children('.element').children('.plusOne');
            var asJpeg = $(this).parent().parent().parent().children('.bFoot').children('.element').children('.asJpeg');
            var toWall = $(this).parent().parent().parent().children('.bFoot').children('.element').children('.toWall');
            
            
            
            $(this).click(function(){
                
                $.ajax({
                    url: "./loadApho.php?aphoId=" + $(this).attr('id') +'&none='+$(this).attr('none'),
                    dataType : "json",
                    success: function(data){
                        
                        
                        
                        $(i).html(data['text']);
                        
                        if(data['text'].length>40 && data['text'].length<250){
                            $(asJpeg).removeClass('hide');
                        }else{
                            $(asJpeg).addClass('hide');
                        }
                        $(asJpeg).attr('href', "./img-"+(1000+Math.floor(Math.random()*9999))+".png?text="+ data['text']+"&backId="+(1+Math.floor(Math.random()*8)));
                        $(i).fadeIn('fast');
                        
                        $(i).attr('id', data['aphorismId']);
                        
                        $(thisHref).attr('none', data['aphorismId'])
//                        var feedUrl = "http://www.facebook.com/dialog/feed?app_id=" 
//                            + <?= $app_id ?> + "&redirect_uri=" + '<?= urlencode($canvas_page) ?>'
//                            + "&message=" + data['theme'] + ": " + data['text'];
//                        
                        $(toWall).attr('href', feedUrl);
                        
                        $(foot).show('fast');
                        $(foot).attr('id', "aph_" +data['aphorismId']);
                        
                        //                      .attr('id', data['aphorismId']);
                    }
                });
            });
            
            $(".asJpeg").each(function(){
//                $(this).click(function(){
//                    var toText=$(this).parent().parent().parent().children('.bBody').html();
//                    document.location.href="./asImage.php?text="+toText+"&backId=<?=rand(1,4)?>";
//                });    
            });
        });
        
        
        
        
    });
</script>

